<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="货号" prop="articleId">
        <el-input
          v-model="queryParams.article"
          placeholder="请输入货号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="码数" prop="size">
        <el-input
          v-model="queryParams.size"
          placeholder="请输入码数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户名称" prop="supplierId">
        <el-input
          v-model="queryParams.supplier"
          placeholder="请输入客户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单编号" prop="orderNumber">
        <el-input
          v-model="queryParams.orderNumber"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.tb_scheduling_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select
          v-model="queryParams.type"
          placeholder="请选择类型"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.tb_scheduling_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

<!--       
      <el-form-item label="日期范围" prop="createTime">
        <el-date-picker type="daterange" v-model="form.field102" format="yyyy-MM-dd"
          value-format="yyyy-MM-dd" :style="{width: '100%'}" start-placeholder="开始日期" end-placeholder="结束日期"
          range-separator="至" clearable></el-date-picker>
      </el-form-item> -->


      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:scheduling:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:scheduling:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:scheduling:remove']"
        >删除</el-button>
      </el-col>
      <div v-if="buttonStatus === true">
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="multiple"
            @click="handleUpdateStatus"
            v-hasPermi="['system:scheduling:remove']"
          >修改状态</el-button>
        </el-col>
      </div>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:scheduling:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="schedulingList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="货号" align="center" prop="articleNumber" />
      <el-table-column label="客户供应商" align="center" prop="supplierId" />
      <el-table-column label="订单编号" align="center" prop="orderNumber" />
      <el-table-column label="类型" align="center" prop="type" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.tb_scheduling_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="码数" align="center" prop="size" />
      <el-table-column label="数量" align="center" prop="number" />
      <el-table-column label="单价" align="center" prop="price" />
      <el-table-column label="总价" align="center" prop="totalPrice" />
      <el-table-column label="状态" align="center" prop="status" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.tb_scheduling_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:scheduling:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:scheduling:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
 
     <!-- 修改处理状态对话框 -->
     <el-dialog :title="title" :visible.sync="openStatus" width="500px" append-to-body>
      <el-form ref="formStatus" :model="formStatus" :rules="rulesStatus" label-width="80px">
        <el-form-item label="状态" prop="status">
          <el-select v-model="formStatus.status" placeholder="请选择状态">
            <el-option
              v-for="dict in dict.type.tb_scheduling_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <div v-if="formStatus.status === '1' ">
            <el-form-item label="运单号" prop="waybillNumber">
              <el-input v-model="formStatus.waybillNumber" placeholder="请输入运单号" />
            </el-form-item>
            <el-form-item label="运费" prop="freight">
              <el-input v-model="formStatus.freight" placeholder="请输入运费" />
            </el-form-item>
          </div>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitFormStatus">确 定</el-button>
          <el-button @click="cancelState">取 消</el-button>
        </div>
      </el-dialog>


    <!-- 添加或修改排单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="品牌" prop="brandId">
          <el-select v-model="form.brandId" @click.native="getBrandList" size="small" placeholder="请输入品牌" @change="changeBrand">
            <el-option v-for="brand in BrandIdList" :key="brand.id" :label="brand.brand"
            :value="brand.id">
          </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="货号" prop="articleId">
          <el-select v-model="form.articleId" @click.native="getListArticleList" size="small" placeholder="请输入货号" @change="changeArticle">
            <el-option v-for="article in articleIdList" :key="article.id" :label="article.articleNumber"
            :value="article.id">
          </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="码数" prop="size">
          <el-select v-model="form.size" @click.native="getArticleList" size="small" placeholder="请输入码数" >
            <el-option v-for="article in articleIdList" :key="article.id" :label="article.size"
            :value="article.size">
          </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择类型">
            <el-option
              v-for="dict in dict.type.tb_scheduling_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <div v-if="form.type === '1' ">
          <el-form-item label="供应商" prop="supplieId">
            <el-select v-model="form.supplierId" @click.native="getSupplierList" size="small" placeholder="请输入供应商">
              <el-option v-for="supplier in supplierList" :key="supplier.id" :label="supplier.name"
              :value="supplier.id">
            </el-option>
            </el-select>
          </el-form-item>
        </div>
        <div v-else-if="form.type === '2'">
            <el-form-item label="客户" prop="supplieId">
              <el-select v-model="form.supplierId" @click.native="getSupplierList" size="small" placeholder="请输入客户">
                <el-option v-for="supplier in supplierList" :key="supplier.id" :label="supplier.name"
                :value="supplier.id">
              </el-option>
              </el-select>
            </el-form-item>
         </div>

        <el-form-item label="数量" prop="number">
          <el-input v-model="form.number" placeholder="请输入数量" />
        </el-form-item>
        <el-form-item label="单价" prop="price">
          <el-input v-model="form.price" placeholder="请输入单价" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listScheduling, getScheduling, delScheduling, addScheduling, updateScheduling, updateSchedulingStatus } from "@/api/system/scheduling";
import { listBrand } from  "@/api/system/brand";
import { listAtricle,listAtriclelist } from  "@/api/system/atricle";
import {listSupplier} from "@/api/system/supplier";

export default {
  name: "Scheduling",
  dicts: ['tb_scheduling_status', 'tb_scheduling_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 排单表格数据
      schedulingList: [],   
      // 添加弹窗品牌数据弹窗
      BrandIdList: [],
      // 添加弹窗货号数据弹窗
      articleIdList: [],
      // 添加客户数据弹窗
      supplierList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示弹出层-处理状态
      openStatus: false,
      // 是否显示处理按钮
      buttonStatus: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        articlceId: null,
        supplierId: null,
        orderNumber: null,
        type: null,
        size: null,
        number: null,
        price: null,
        totalPrice: null,
        status: null,
        createId: null,
        updateId: null
      },
      params: {
        pageNum: 1,
        pageSize: 800,
      },
      paramsByAtricle: {
        brandId: null,
        articleNumber: null
      },
      // selectItem:'',
      // 表单参数
      form: {},
      formStatus:{},
      // 表单校验
      rulesStatus: {
      },
      // 表单校验
      rules: {
        articleId: [
          { required: true, message: "货号不能为空", trigger: "blur" }
        ],
        supplierId: [
          { required: true, message: "客户供应商不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "类型不能为空", trigger: "change" }
        ],
        size: [
          { required: true, message: "码数不能为空", trigger: "blur" }
        ],
        number: [
          { required: true, message: "数量不能为空", trigger: "blur" }
        ],
        price: [
          { required: true, message: "单价不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询排单列表 */
    getList() {
      this.loading = true;
      listScheduling(this.queryParams).then(response => {
        this.schedulingList = response.data.records;
        this.total = response.data.totalCount;
        this.loading = false;
      });
      if(this.queryParams.status!=null){
        console.log(1111);
        this.buttonStatus = true;
      };
    },
    /** 查询品牌列表 */
    getBrandList() {
      this.loading = true;
      listBrand(this.params).then(response => {
        this.BrandIdList = response.data.records;
        this.loading = false;
      });
    },
     /** 查询供应商列表 */
     getSupplierList() {
      this.loading = true;
      listSupplier(this.params).then(response => {
        this.supplierList = response.data.records;
        this.loading = false;
      });
    },
    /** 查询货号列表 */
    getArticleList() {
      this.loading = true;
      listAtricle(this.paramsByAtricle).then(response => {
        this.articleIdList = response.data.records;
        this.loading = false;
      });
    },
    getListArticleList() {
      this.loading = true;
      listAtriclelist(this.paramsByAtricle).then(response => {
        this.articleIdList = response.data;
        this.loading = false;
      });
    },
    /** change保存品牌 */
    changeBrand(val){
      this.paramsByAtricle.brandId = val
    },
    /**change保存货号 */
    changeArticle(val){
      var obj = {};
      obj = this.articleIdList.find(function(item){
        if(item.id === val){
          return item;
        }
      })
      this.paramsByAtricle.articleNumber = obj.articleNumber
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消按钮
    cancelState() {
      this.openStatus = false;
      this.resetStatus();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        articleId: null,
        supplieId: null,
        orderNumber: null,
        type: null,
        size: null,
        number: null,
        price: null,
        totalPrice: null,
        status: "0",
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        delFlag: null,
        createId: null,
        updateId: null
      };
     
      this.resetForm("form");
    },
    // 表单重置
    resetStatus() {
      this.formStatus = {
        id: null,
        status: null,
        freight: null,
        waybillNumber: null
      };
      this.resetForm("formStatus");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加排单";
    },
    /** 处理状态按钮操作 */
    handleUpdateStatus(row) {
      const id = row.id || this.ids
      this.formStatus.id=id;
      this.openStatus = true;
      this.title = "修改处理状态";
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getScheduling(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改排单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateScheduling(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addScheduling(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 提交按钮 */
    submitFormStatus() {
      this.$refs["formStatus"].validate(valid => {
        console.log(0);
        if (valid) {
          console.log(1);
          if (this.formStatus.id != null) {
            console.log(2);
            this.formStatus.id = this.formStatus.id.toString();
            updateSchedulingStatus(this.formStatus).then(response => {
              console.log(3);
              this.$modal.msgSuccess("修改成功");
              this.openStatus = false;
              this.getList();
            });
          } 
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除排单编号为"' + ids + '"的数据项？').then(function() {
        return delScheduling(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/scheduling/export', {
        ...this.queryParams
      }, `scheduling_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
